using System;
using System.Collections.Generic;
using HTCollections.Unsafe;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace HTCollections.Examples.Sort
{
    public class SortExample : MonoBehaviour
    {
        public bool IsIncrease = true;
        public List<int> IntLst = new List<int>();
        public int RandomNumCount;

        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Q))
            {
                UnsafeArray<int> array = new UnsafeArray<int>(IntLst.Count);
                for (int i = 0; i < IntLst.Count; i++)
                    array[i] = IntLst[i];
                array.Sort_Bubble((a1, a2) => IsIncrease ? a1 > a2 : a1 < a2);
                Debug.Log("Array 排序后:");
                foreach (var t in array)
                    Debug.Log($"{t}");
                array.Dispose();
            }

            if (Input.GetKeyDown(KeyCode.W))
            {
                UnsafeList<UnsafeListItem> list = new UnsafeList<UnsafeListItem>(IntLst.Count);
                for (int i = 0; i < IntLst.Count; i++)
                {
                    list.Add(new UnsafeListItem()
                    {
                        Value = IntLst[i],
                        FloatValue = i * 1f,
                        BoolValue = i % 2 == 0,
                    });
                }

                list.Sort_Bubble((a1, a2) => IsIncrease ? a1.Value > a2.Value : a1.Value < a2.Value);
                Debug.Log("List 排序后:");
                foreach (var t in list)
                    Debug.Log($"{t}");
                list.Dispose();
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                UnsafeArray<int> array = new UnsafeArray<int>(IntLst.Count);
                for (int i = 0; i < IntLst.Count; i++)
                    array[i] = IntLst[i];
                array.Sort_Quick((a1, a2) => IsIncrease ? a1 > a2 : a1 < a2);
                Debug.Log("Array 排序后:");
                foreach (var t in array)
                    Debug.Log($"{t}");
                array.Dispose();
            }

            if (Input.GetKeyDown(KeyCode.S))
            {
                UnsafeList<UnsafeListItem> list = new UnsafeList<UnsafeListItem>(IntLst.Count);
                for (int i = 0; i < IntLst.Count; i++)
                {
                    list.Add(new UnsafeListItem()
                    {
                        Value = IntLst[i],
                        FloatValue = i * 1f,
                        BoolValue = i % 2 == 0,
                    });
                }

                list.Sort_Quick((a1, a2) => IsIncrease ? a1.Value > a2.Value : a1.Value < a2.Value);
                Debug.Log("List 排序后:");
                foreach (var t in list)
                    Debug.Log($"{t}");
                list.Dispose();
            }

            if (Input.GetKeyDown(KeyCode.Z))
            {
                // List<int> lst = new List<int>(RandomNumCount);
                // for (int i = 0; i < RandomNumCount; i++)
                //     lst.Add(Random.Range(0, RandomNumCount * 3 + 1));
                //
                // UnsafeArray<int> array1 = new UnsafeArray<int>(lst.Count);
                // for (int i = 0; i < lst.Count; i++)
                //     array1[i] = lst[i];
                // Stopwatch sw1 = new Stopwatch();
                // sw1.Start();
                // array1.Sort_Quick((a1, a2) => IsIncrease ? a1 > a2 : a1 < a2);
                // sw1.Stop();
                // Debug.Log($"Array 快速排序 fast 耗时: {sw1.Elapsed.TotalMilliseconds} ms");
                //
                // UnsafeArray<int> array2 = new UnsafeArray<int>(lst.Count);
                // for (int i = 0; i < lst.Count; i++)
                //     array2[i] = lst[i];
                // Stopwatch sw2 = new Stopwatch();
                // sw2.Start();
                // array2.Sort_QuickS((a1, a2) => IsIncrease ? a1 > a2 : a1 < a2);
                // sw2.Stop();
                // Debug.Log($"Array 快速排序 slow 耗时: {sw2.Elapsed.TotalMilliseconds} ms");
                //
                // bool isSame = true;
                // for (int i = 0; i < array1.Length; i++)
                // {
                //     if (array1[i] != array2[i])
                //         isSame = false;
                // }
                // Debug.Log($"结果: {(isSame?"一致":"不一致")}");
                // array1.Dispose();
                // array2.Dispose();
            }
        }

        struct UnsafeListItem : IEquatable<UnsafeListItem>
        {
            public int Value;
            public float FloatValue;
            public bool BoolValue;

            public bool Equals(UnsafeListItem other)
            {
                return Value == other.Value;
            }

            public override bool Equals(object obj)
            {
                return obj is UnsafeListItem other && Equals(other);
            }

            public override int GetHashCode()
            {
                return HashCode.Combine(Value, FloatValue, BoolValue);
            }

            public override string ToString()
            {
                return $"{Value},{FloatValue},{BoolValue}";
            }
        }
    }
}